package com.le.edu.utils;

import cn.hutool.core.util.StrUtil;
import com.le.edu.exception.AppException;
import com.le.edu.resp.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;

/**
 * @Author Le
 * @Create 2024-01-14 20:25
 */
public class ClientUtils {

    public enum ClientEnum {
        /**
         * 客户端类型
         */
        ADMIN_CLIENT(1, "ADMIN"),
        USER_CLIENT(2, "USER");

        private Integer code;
        private String typeName;

        ClientEnum(Integer code, String typeName) {
            this.code = code;
            this.typeName = typeName;
        }
    }

    public static ClientEnum checkClientType(HttpServletRequest request) {
        String type = request.getHeader(ConstUtils.ClientType);
        if (StrUtil.isBlank(type)) {
            throw new AppException(ResultCodeEnum.APP_MEMBER_INVAILD);
        }

        // 判断客户端类型
        for (ClientEnum value : ClientEnum.values()) {
            if (type.equals(value.typeName)) {
                return value;
            }
        }

        throw new AppException(ResultCodeEnum.APP_MEMBER_INVAILD);
    }
}
